package com.liwenfeng.diancanspringboot.aspect;

import com.liwenfeng.diancanspringboot.annotation.AutoFill;
import com.liwenfeng.diancanspringboot.common.context.BaseContext;
import com.liwenfeng.diancanspringboot.common.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDateTime;

/**
 * 自定义切面，实现公共字段自动填充的处理逻辑
 * */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    /**
     * 切入点
     * */
    @Pointcut("execution(* com.liwenfeng.diancanspringboot.mapper.*.*(..)) && @annotation(com.liwenfeng.diancanspringboot.annotation.AutoFill)")
    public void autoFillPointCut(){}

    /**
     * 前置通知，在通知中进行公共字段的赋值
     * */
    @Before("autoFillPointCut()")
    public void doAutoFill(JoinPoint joinPoint){
        log.info("开始进行公共字段自动填充");

        // 获取到当前被拦截的方法上的数据库操作类型
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        OperationType operationType = methodSignature.getMethod().getAnnotation(AutoFill.class).value();

        // 获取到当前被拦截的方法的参数，即实体对象
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0){
            return;
        }
        Object entity = args[0];
        //准备赋值的数据
        Long currentId = BaseContext.getCurrentId();
        LocalDateTime now = LocalDateTime.now();
        // 根据当前数据库操作的类型，为对应的属性进行赋值
        switch (operationType){
            case INSERT:
                // 插入操作，为实体对象对应的各个属性赋值
                try {
                    Method setCreateTime = entity.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class);
                    setCreateTime.invoke(entity, now);
                    Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime",LocalDateTime.class);
                    setUpdateTime.invoke(entity, now);
                    Method setCreateUser = entity.getClass().getDeclaredMethod("setCreateUser", Long.class);
                    setCreateUser.invoke(entity, currentId);
                    Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser",Long.class);
                    setUpdateUser.invoke(entity, currentId);
                } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            case UPDATE:
                // 更新操作，为实体对象对应的各个属性赋值
                try {
                    Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class);
                    setUpdateTime.invoke(entity, now);
                    Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser",Long.class);
                    setUpdateUser.invoke(entity, currentId);
                } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
        }
    }
}
